package com.ciwong.epaper.util;

/**
 * File Name：LogOutUtil.java
 * Description： LogOutUtil.java
 * Created by res-mingyang on 2015/8/7.
 */

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.epaper.lsdao.LSDao;
import com.ciwong.epaper.modules.me.dao.MeDao;
import com.ciwong.epaper.modules.me.dao.db.db.StudyRecordDB;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.modules.me.util.UploadFileHelper;
import com.ciwong.epaper.modules.pad.contentprovider.LoginToken;
import com.ciwong.epaper.modules.scan.dao.ScanDao;
import com.ciwong.epaper.util.download.DownLoad;
import com.ciwong.epaper.util.download.DownLoadDB;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.mobilelib.utils.BaseActivityManager;
import com.ciwong.mobilelib.utils.BaseConstants;
import com.ciwong.mobilelib.utils.ThreadTask;

import java.io.IOException;

/**
 * 注销的util
 *
 * @author RES-KUNZHU
 * @version ciwong v.1.0 2014-5-28
 * @since ciwong v.1.0
 */
public class LogOutUtil {
    /**
     * 注销
     *
     * @param context 上下文
     */
    public static void logOut(Activity context) {
        logOut(context, false);
    }

    /**
     * 注销
     *
     * @param context 上下文
     */
    public static void logOut(Activity context, boolean killProgress) {
        EApplication application = (EApplication) context.getApplication();
        //退出登录时，把当前用户是否绑定手机号的信息清掉
        try {
            CWSys.setSharedString(EConstants.SHARE_PRE_CURR_LOGIN_USER_PHONE + SerializableManager.getInstance().getUserId(true), "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        CWSys.setSharedLong(EConstants.SHARE_PRE_CURR_LOGIN_USER, 0);
        try {
            CWSys.setSharedSerializable(BaseConstants.SHARE_KEY_VERITIFY_INFO, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        clearPersonalSp();
        HttpRequest.setVerifyInfo(null);
        application.setServiceDetails(null);
        application.setServiceDetail(null);
        application.setSchoolDetail(null);
        application.setClazzs(null);
        application.setClazz(null);
        application.setGrade(null);
        if (BaseActivityManager.getInstance().getMainActivity() != null) {
            BaseActivityManager.getInstance().getMainActivity().finish();
        }
        //退出登录，清除token，发广播
        LoginToken.exitLogin(context);
        if (!killProgress) {
            if (EApplication.PLATFORM_ENV != EApplication.PLATFORM_PAD) {
                MeJumpManager.jumpToLogin(context);
            } else {

                MeJumpManager.jumpToSplash(context);
            }
        }
        doExit(killProgress, context);
    }

    private static void clearPersonalSp() {

        try {
            CWSys.setSharedSerializable(BaseConstants.SHARE_KEY_USER_INFO_BASE, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            CWSys.setSharedSerializable(SerializableManager.SerializeKey.SHARE_KEY_CHOOSE_SCHOOL + SerializableManager.getInstance().getUserId(true), null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            CWSys.setSharedSerializable(
                    SerializableManager.SerializeKey.SHARE_KEY_CLASS_DEFAULT
                            + SerializableManager.getInstance()
                            .getUserId(true), null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            CWSys.setSharedSerializable(
                    SerializableManager.SerializeKey.SHARE_KEY_SERVICE_DETAIL_LIST
                            + SerializableManager.getInstance()
                            .getUserId(true), null);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 退出
     *
     * @param killProgress 是否杀掉进程
     * @param context      上下文
     */
    private static void doExit(boolean killProgress, Context context) {
        ThreadTask.getInstance().shutDownAll();
        logOutDao();
        logOutDB();
        BaseActivityManager.getInstance().killAllActivity();
        if (killProgress) {
            System.exit(0);
        }
    }

    /**
     * 释放dao
     */
    public static void logOutDao() {
        UploadFileHelper.getInstance().release();
        MeDao.getInstance().release();
        EpaperDao.getInstance().release();
        ScanDao.getInstance().release();
        LSDao.getInstance().release();
    }

    /**
     * 注销数据库
     */
    public static void logOutDB() {
        // 停止下载任务并保存状态
        Log.i("lqi", "-------------------------LOG OUT DB----------------------------------------------------------");
        DownLoad.getInstance().stopTaskList();
        DownLoadDB.logOut();
        StudyRecordDB.logOut();
    }
}
